# -*- coding:utf-8 -*-


"""
@version: v1.0
@author: 夏伟坤
@Email: 915624876@qq.com
@software: PyCharm
@project: CXX_UploadFile
@file: OF_UpdateRedis.py
@time: 2021/10/29 10:17
"""
import json
import uuid

from flask import views, jsonify, request

from core.db.db_redis import r3
from core.utils.log_helper import logger


class UF_GetFileInfo(views.MethodView):

    def __init__(self):
        pass

    # 返回值封装
    def _render(self, code=None, msg=None):
        logger.exception("返回值：错误代码：%s,错误信息：%s" % (code, msg))

        return jsonify({
            'code': code,
            'msg': msg
        })

    def get(self):
        return self._render(-700, '/api/UF_GetFileInfo接口请求方式仅限为POST')

    def post(self):
        """
        传入格式
        :return:
        """

        class FileStatus(object):
            waiting_upload = 0  # 待上传
            upload_complete = 1  # 上传完成
            upload_failed = 2  # 上传失败

        try:

            # 接收文件路径，文件大小
            result = request.form
            inputjson = result.get('inputjson', '')
            size = result.get('size', '')
            # logger.info(result)
            # logger.info(input_result)

            if not inputjson:
                self._render(703, '未传入参数inputjson')
            elif not size:
                self._render(703, '未传入参数size')
            else:
                try:
                    inputjson = json.loads(inputjson)
                    filesize = inputjson[0].get('filesize', '')
                    if not filesize:
                        self._render(702, '没有接收到filesize参数')
                    filepath = inputjson[0].get('filepath', '')
                    if not filepath:
                        self._render(703, '没有接收到filepath参数')
                except Exception as e:
                    logger.exception("参数校验失败")
                    self._render(704, "参数校验失败")
                else:
                    logger.info("传入文件信息为：%s" % inputjson)

                    list1 = []
                    # dict1 = {}
                    try:
                        for i in inputjson:
                            filesize = i.get('filesize', '')
                            filepath = i.get('filepath', '')
                            filename = filepath.split('\\')[-1]

                            data = {
                                'filename': filename,
                                'filesize': filesize,
                                'filepath': filepath,
                                'status': FileStatus.waiting_upload
                            }
                            list1.append(data)
                        dict1 = {
                            'fileinfo': list1,
                            'size': size
                        }
                    except Exception as e:
                        logger.exception('获取文件信息错误')
                        self._render(705, '获取文件信息错误')
                    else:
                        # 生成批次号
                        uuid_ = str(uuid.uuid1())
                        # 存redis
                        r3.hmset(uuid_, dict1)

                        logger.info("调用签到接口返回的信息为：%s" % json.dumps({
                            'code': 1,
                            'msg': '',
                            'uuid_number': uuid_
                        }, ensure_ascii=False))
                        return jsonify({
                            'code': 1,
                            'msg': '',
                            'uuid_number': uuid_
                        })
        except Exception as e:
            logger.exception(e)
            self._render(704, "/api/UF_GetFileInfo接口调用失败")


if __name__ == '__main__':
    pass
